package me.magicall.test.net;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import me.magicall.consts.StrConst.EncodingConst;
import me.magicall.util.kit.Kits;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

/**
 * @author MaGiCalL
 * @email wenjian.liang@opi-corp.com
 * @version Jul 7, 2011 2:57:24 PM
 */
public class ToHttpClient {

//	Host	www.baidu.com
//	User-Agent	Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9
//	Accept	text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//	Accept-Language	zh-cn,zh;q=0.5
//	Accept-Encoding	gzip,deflate
//	Accept-Charset	GB2312,utf-8;q=0.7,*;q=0.7
//	Keep-Alive	115
//	Connection	keep-alive
//	Referer	http://www.baidu.com/s?wd=org.apache.http&oq=org.a&sug=org.apache.axis.client.call&n=1&rsp=8&inputT=11967
//	Cookie	BAIDUID=F5A12EE14B0947A902DDA201D3D8626E:FG=1; USERID=73ce900a9e32283a043bc7f15d48; OPENPLATFORM_SP=9008d3efcec4cdcfbaf3cdc80700_1308192148; BDUSS=zBZNXE3UTE4VE1XbFFIcXFlNk12TkxlZnlqQkRtM1I5b0tER0hBbERLdHR5aUpPQUFBQUFBJCQAAAAAAAAAAAoavCuQCAcA0-~OxM3PuvPNyAAAAAAAAAAAAAAAAAAAAAAAAAAAAADgusV6AAAAAOC6xXoAAAAAcF1CAAAAAAAxMC4yMy4yNG49-01tPftNT; IM_=1; BDSFRCVID=GtF_JgCgyK7vZmypMKd_MzdzmVpHbgwP3J
//	Cache-Control	max-age=0

	public static HttpParams newHttpParams() {
		// 默认的HTTP头信息
		final HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, EncodingConst.GBK);
		HttpProtocolParams.setUseExpectContinue(params, true);
		HttpProtocolParams.setUserAgent(params, "Mozilla/5.0 Gecko/20100722 Firefox/3.6.8"); // 就用Firefox的。哈哈。

		HttpConnectionParams.setTcpNoDelay(params, true);
		HttpConnectionParams.setSocketBufferSize(params, 8192);

		return params;
	}

	public static byte[] getContentAsByteArr(final String url) {
		return getContentAsByteArr(url, newHttpParams());
	}

	public static byte[] getContentAsByteArr(final String url, final Map<String, String> paramsMap) {
		return getContentAsByteArr(url, buildParams(paramsMap));
	}

	public static byte[] getContentAsByteArr(final String url, final HttpParams params) {
		final HttpEntity entity = getHttpEntity(url, params);
		try {
			return entity == null ? Kits.BYTE.emptyPrimitiveArray() : EntityUtils.toByteArray(entity);
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return Kits.BYTE.emptyPrimitiveArray();
	}

	public static String getContent(final String url) {
		return getContent(url, EncodingConst.GBK);
	}

	public static String getContent(final String url, final String encoding) {
		return getContent(url, newHttpParams(), encoding);
	}

	public static String getContent(final String url, final Map<String, String> paramsMap, final String encoding) {
		return getContent(url, buildParams(paramsMap), encoding);
	}

	public static String getContent(final String url, final HttpParams params, final String encoding) {
		final HttpEntity entity = getHttpEntity(url, params);
		try {
			return entity == null ? Kits.STR.emptyValue() : EntityUtils.toString(entity, encoding);
		} catch (final ParseException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return Kits.STR.emptyValue();
	}

	public static HttpEntity getHttpEntity(final String url) {
		return getHttpEntity(url, newHttpParams());
	}

	public static HttpEntity getHttpEntity(final String url, final Map<String, String> paramsMap) {
		final HttpParams params = buildParams(paramsMap);
		return getHttpEntity(url, params);
	}

	private static HttpParams buildParams(final Map<String, String> paramsMap) {
		final HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		for (final Entry<String, String> entry : paramsMap.entrySet()) {
			params.setParameter(entry.getKey(), entry.getValue());
		}
		return params;
	}

	public static HttpEntity getHttpEntity(final String url, final HttpParams params) {
		final HttpGet getMethod = new HttpGet(url);
		try {
			final HttpClient httpClient = new DefaultHttpClient(params);
			final HttpResponse response = httpClient.execute(getMethod);
			return response.getEntity();
		} catch (final ClientProtocolException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(final String[] args) {
		final String s = getContent("http://music.qq.com/portal_v2/static/list/singerlist_1.html");
		System.out.println("@@@@@@" + s.length());
	}
}
